const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtract = require('mini-css-extract-plugin')

const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
  entry: path.resolve(__dirname, '../src/index.js'),
  output: {
    filename: 'js/bundle.js',
    path: path.resolve(__dirname, '../dist')
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../src'),
      modules: [
        path.resolve(__dirname, 'myLib'),
        path.resolve(__dirname, '../node_modules')
      ],
    },
    extensions: ['', '.ts', '.tsx', '.webpack.js', '.web.js', '.js', '.jsx', '.json'],
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [{
          loader: MiniCssExtract.loader,
          options: {
            publicPath: '../'
          }
        }, 'css-loader', 'postcss-loader']
      },
      {
        test: /\.less$/,
        use: [{
          loader: MiniCssExtract.loader,
          options: {
            publicPath: '../'
          }
        }, 'css-loader', 'postcss-loader', 'less-loader']
      },
      {
        test: /\.html$/,
        loader: 'html-withimg-loader'
      },
      {
        test: /\.(png|jpeg|jpg)$/,
        use: [{
          loader: 'file-loader',
          options: {
            esModule: false,
            outputPath: 'img/'
          }
        }]
      },
      {
        test: /\.js$/,
        include: path.resolve(__dirname, '../src'),
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-transform-runtime']
          }
        }]
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env'],
            ]
          }
        }
      },
      {
        test: /\.(ts|tsx)$/,
        exclude: /node-modules/,
        use: [
          'babel-loader', 'ts-loader'
        ]
      },
      {
        test: require.resolve('three/examples/js/controls/OrbitControls'),
        use: 'imports-loader?THREE=three'
      },
      {
        test: require.resolve('three/examples/js/controls/OrbitControls'),
        use: 'exports-loader?THREE.OrbitControls'
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../public/index.html'),
      filename: 'index.html'
    }),
    new MiniCssExtract({
      filename: 'css/index.css'
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, '../static'),
          to: path.resolve(__dirname, '../dist/static')
        }
      ]
    })
  ],
}